package com.dao;

import com.beans.Goodsinfo;
import com.beans.PageInfo;
import com.jdbc.DBUtil;

import java.util.List;

public class GoodsDao {
    public static int getGoodsCount(String bigC,String smallC,String goods) {
        String sql="select count(*) from goodsinfo where 1=1 ";
        if((!"".equals(bigC))&&bigC!=null){
            sql+=" and bigCateId="+bigC;
        }
        if((!"".equals(smallC))&&smallC!=null){
            sql+=" and smallCateId="+smallC;
        }
        if((!"".equals(goods))&&goods!=null){
            sql+=" and goodsName like '%"+goods+"%' ";
        }
        System.out.println("1:"+sql);
        long i= DBUtil.getScalar(sql);
        return (int)i;
    }

    public List<Goodsinfo> getGooodsList(PageInfo page,String bigC,String smallC,String goods) {
        String sql="select * from goodsinfo where 1=1 ";
        if((!"".equals(bigC))&&bigC!=null){
            sql+=" and bigCateId="+bigC;
        }
        if((!"".equals(smallC))&&smallC!=null){
            sql+=" and smallCateId="+smallC;
        }
        if((!"".equals(goods))&&goods!=null){
            sql+=" and goodsName like '%"+goods+"%' ";
        }
        sql+=" limit ?,?";
        System.out.println("2:"+sql);
        return DBUtil.getList(sql,Goodsinfo.class,page.getBeginRow(),page.getPageSize());
    }

    public int del(int i) {
        return DBUtil.update("delete from goodsinfo where id=?",i);
    }

    public int addGoods(Goodsinfo goods) {
        String sql="insert into goodsinfo (goodsName,bigCateId,smallCateId,price,unit, pictureData,producter,des) values(?,?,?,?,?,?,?,?) ";
        return DBUtil.update(sql,goods.getGoodsName(), goods.getBigCateId(), goods.getSmallCateId(), goods.getPrice(), goods.getUnit(), goods.getPictureData(), goods.getProducter(), goods.getDes());
    }

    public Goodsinfo getAdminNamebyid(String name) {
        return DBUtil.getSingleObj("select * from goodsinfo where goodsName=?",Goodsinfo.class,name);
    }

    public Goodsinfo getgoodsbyid(int id) {
        return DBUtil.getSingleObj("select * from goodsinfo where id=?",Goodsinfo.class,id);
    }

    public int update(Goodsinfo goods) {
        return DBUtil.update("update from goodsinfo set goodsName=?,bigCateId=?,smallCateId=?,price=?,unit=?, pictureData=?,producter=?,des=?",
                goods.getGoodsName(), goods.getBigCateId(), goods.getSmallCateId(), goods.getPrice(), goods.getUnit(), goods.getPictureData(), goods.getProducter(), goods.getDes()
        );
    }
}
